import { defineStore } from "pinia";
import { ref } from "vue";
import { type UserLoginRequest } from "@/api/user/userLoginRequest";
import { userLogin } from "@/api/user/userLogin";
import { message } from "ant-design-vue";
import router from "@/router";
type  User = {
  userName: string;
  token: string;

};
export const useLoginUserStore = defineStore("loginUser", () => {
  const loginUser = ref<User>({
    userName: "未登录",
    token:""
  });

  async function fetchLoginUser(data: UserLoginRequest) {
    const res = await userLogin.login(data);
    console.log('res.data', res)
    if (res.code == 0) {
      loginUser.value = res.data;
       message.success("登录成功");
      router.push({
        path: "/",
        replace: true,
      })
    }
  


  }

  function setLoginUser(newLoginUser: any) {
    loginUser.value = newLoginUser;
  }

  return { loginUser, setLoginUser, fetchLoginUser };
}, 
{
  // pinia持久化插件开启
  persist: true
});
